/*加载我的商品*/
function Goods() {
    $("#mine_goods").attr("class", "textred");
    $("#send_goods").attr("class", "TextGray");
    $("#wait_pay").attr("class", "TextGray");
    $("#left_line").attr("class", "left_line");
    $("#right_line").attr("class", "right_line");
    $("#center_line").attr("class", "right_line");
    $("#not_data").hide();
    get_goods_info(0);

}
/*加载待支付*/
function wait_pay() {
    $("#mine_goods").attr("class", "TextGray");
    $("#send_goods").attr("class", "TextGray");
    $("#wait_pay").attr("class", "textred");
    $("#left_line").attr("class", "right_line");
    $("#right_line").attr("class", "right_line");
    $("#center_line").attr("class", "left_line");
    $("#not_data").hide();
    get_indent_info(0, 1);
}
/*加载已发货*/
function SendGoods() {
    $("#mine_goods").attr("class", "TextGray");
    $("#send_goods").attr("class", "textred");
    $("#wait_pay").attr("class", "TextGray");
    $("#left_line").attr("class", "right_line");
    $("#right_line").attr("class", "left_line");
    $("#center_line").attr("class", "right_line");
    $("#not_data").hide();
    get_indent_info(0, 2);
}

$(function () {
    var state = $("#state").val();
    if (state == 1)
        wait_pay()
    else if (state == 2)
        SendGoods()
    else
        Goods();
});

function get_goods_info(page) {
    $("#state").val(0);
    $("#repertory_good").show();
    $("#repertory_indent").hide();
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/shopindent.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            page: page
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var datas = data.data;
                var goodinfo = "";
                $(datas).each(function () {
                    goodinfo += '<div class="goods" onclick="location.href=\'/except/files/shop/reptory_shop_info.jsp?repertoryid=' + this.repertory_id + '\'">' +
                        // '<img src="' + this.img + '" class="item_img" onerror="imgerror(this,\'img/onerror.png\');"/>' +

                        '<div style="float: left;margin-right: 10px; width:85px; height:85px;overflow-y: hidden; ' +
                        'background: url(' + this.img + ') no-repeat center;background-size: cover;"' +
                        ' onerror="imgerror(this,\'img/onerror.png\');"></div>' +

                        '<ul class="item_ul">' +
                        '<li>' +
                        '<span class="item_name">' + this.shop_name + '</span>' +
                        '</li>' +
                        '<li style="margin-top: 10px;">' +
                        '<span class="item_desc">' + this.name + '</span>' +
                        '</li>' +
                        '<li>' +
                        '<div >' +
                        '<span class="item_money">￥：<i>' + this.price + '</i></span>' +
                        '<span class="item_buy_num">还剩' + this.surplusnum + '件</span>' +
                        '</div>' +
                        '</li>' +
                        '</ul>' +
                        '</div>' +
                        '<hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;" />' +
                        '</a>' +
                        '<div class="footer">' +
                        '<span onclick="location.href=\'/except/files/user/my_shop_address.jsp?shoptype=1\'">我要发货</span>' +
                        '</div>';
                });
            } else if (code == 20013) {
                if (page == 0) {
                    $("#not_data").show();
                }
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
            if (page == 0)
                $("#repertory_good").empty().append(goodinfo);
            else
                $("#repertory_good").append(goodinfo);
            $("#page").val(page * 1 + 1);
        }
    });
}

function get_indent_info(page, type) {
    $("#state").val(type);
    $("#repertory_good").hide();
    $("#repertory_indent").show();
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var msg = "";
    if (type == 1) {
        indentstate = 1;
        msg = "待支付";
    } else {
        msg = "已发货";
        indentstate = 3;
    }
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/indentlist.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            page: page,
            indentstate: indentstate,
            indenttype: "2,3"
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var datas = data.data;
                var goodinfo = "";
                $("#spare_money_show").html("余额(￥" + data.sparemoney + ")");
                $(datas).each(function () {
                    // var indentinfo = this.indentinfo;
                    // $(indentinfo).each(function () {
                    goodinfo += '<div class="store_name" onclick="location.href=\'/except/files/indent/orderinfo.jsp?indentnum=' + this.indent_num + '&repertorytype=' + type + '\'">' +
                        '<div style="float: left;">' +
                        '<span style="margin-left: 10px;font-size: 16px;">我的仓储</span>' +
                        '<img src="img/right.png" class="add_img" />' +
                        '</div>' +
                        '<div style="float: right;">' +
                        '<span class="right_img">' + msg + '</span>' +
                        '</div>' +
                        '</div>' +
                        '<hr style="height: 1px; width: 100%; background-color: #f4f4f4; border: none;" />';
                    var number = 0;
                    $(this.indentshop).each(function () {
                        goodinfo += '<div class="goods">' +
                            '<img src="' + this.img + '" onerror="imgerror(this,\'img/onerror.png\');" class="item_img" />' +
                            '<ul class="item_ul">' +
                            '<li><span class="item_name">' + this.shop_name + '</span></li>' +
                            '<li>' +
                            '<div style="margin-top: 10px;">' +
                            '<span class="item_desc">' + this.name + '</span>' +
                            '<span class="item_buy_num">X' + this.num + '</span>' +
                            '</div>' +
                            '</li>' +
                            '<li><span class="item_money">￥: <i style="font-size: 16px;">' + this.price + '</i></span></li>' +
                            '</ul>' +
                            '</div>';
                        number += this.num * 1;
                    });
                    goodinfo += '<hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;" />' +
                        '<div class="trans_div">' +
                        '<span class="tran_span">(含运费' + this.freight_money + '元)</span>' +
                        '<span class="tran_money">共' + number + '件商品 合计：￥' + this.paymoney + '</span>' +
                        '</div>' +
                        '<hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;" />' +
                        '<div class="trans_div">' +
                        '<div style="float: right;">';
                    if (type == 1) {
                        goodinfo += '<span class="left" onclick="canclerepertory(\'' + this.indent_num + '\');">取消订单' +
                            '</span><span class="right" onclick="repertorypay(\'' + this.indent_num + '\',\'' + this.paymoney + '\', ' + data.sparemoney + ')">立即支付</span>';
                    } else {
                        goodinfo += '<span class="right">查看物流</span>';
                    }
                    goodinfo += '</div>' +
                        '</div>';
                });
                // });

                remove_advertising();
            } else if (code == 20013) {
                if (page == 0) {
                    $("#not_data").show();
                }
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
            if (page == 0)
                $("#repertory_indent").empty().append(goodinfo);
            else
                $("#repertory_indent").append(goodinfo);
            $("#page").val(page * 1 + 1);
        }
    });
}

$(window).scroll(function () {
    var state = $("#state").val();
    //$(document).scrollTop() 获取垂直滚动的距离
    //$(document).scrollLeft() 这是获取水平滚动条的距离
    if ($(document).scrollTop() <= 0) {
        if (state == 0)
            get_goods_info(0);
        else
            get_indent_info(0, state);
    }
    if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
        var page = $("#page").val();
        if (state == 0)
            get_goods_info(page);
        else
            get_indent_info(page, state);
    }
});

/**
 * 取消订单
 */
function canclerepertory(indentnum) {
    var token = $("#token").val();
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/cancleRepertory.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                get_indent_info(0, 1);
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        }
    });
}

function repertorypay(indentnum, paymoney, sparemoney) {

    if ($(".cqchoosenum").hasClass("pop_payss")) {
        $(".cqchoosenum").removeClass("pop_payss");
        $("#background").hide();
    } else {
        $(".cqchoosenum").addClass("pop_payss");
        $("#background").show();
    }

    $("#paymoney").val(paymoney);
    $("#paybtn").attr("onclick", "pay('" + indentnum + "')");
    if (paymoney * 100 <= sparemoney) {
        $("#sparemoney").attr("checked", true);
        $("#wechat").attr("checked", false);
    } else {
        $("#sparemoney").attr("checked", false).attr("disabled", "disabled");
        $("#wechat").attr("checked", true);
    }
}

var payflag = false;
function pay(indentnum) {
    var requrl = location.href.split('#')[0];
    $(".cqchoosenum").removeClass("pop_payss");
    $("#background").hide();
    if (payflag)
        return;
    payflag = false;
    var url = "/except/indentcontroller/payindent.action";
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var paytype = $("#paytype :radio:checked").val();

    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            paytype: paytype,
            indentnum:indentnum
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                confirm = true;
                $("#nonce_str").val(data.data.nonce_str);
                $("#result_sign").val(data.data.result_sign);
                $("#timestamp").val(data.data.indenttime);
                $("#prepay_id").val(data.data.prepay_id);
                $("#payinfo").submit();
                //TODO 判断支付类型，并执行相关操作
                // if(paytype == 1)
                //     location.href = "";
                // else {
                //
                //     //跳转至微信支付
                // }
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show();
            $("#hint_message").html("网络异常");
        }
    });
}